<?php
function mb_str_replace($find,$replace,&$str)
{
	$i = mb_strpos($str,$find, 0,"UTF-8");
	if ($i===false) {return;}
	$str = mb_substr($str, 0,$i).$replace.mb_substr($str, $i+mb_strlen($find,"UTF-8"),mb_strlen($str,"UTF-8"));
	$this->mb_str_replace($find,$replace,$str);
}
/**
 * Eng
 *
 * Класс Eng обрабатывает данные, связанные с изучением английских слов
 *
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 1.1
 */
class Eng extends ActiveRecord {
	public $tableName = 'eng_file';
	public $pkName = 'ID_eng_file';
	
	public $arrPronouns = array('I', 'you', 'we', 'he', 'she', 'it', 'they', 'my', 'your', 'our', 'their', 'his', 'her', 'its', 'me', 'us', 'them'); // type = 1
	public $arrArticles = array('a', 'an', 'the'); // type = 2 
	public $arrNumbers = array( // type = 3
		'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 
		'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen',
		'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred' 
	);


	/**
	 * Сохраняем содержимое файла в БД
	 * 
	 * @param string $strFileContent
	 * @param string $fileName
	 * @return string
	 */
	public function saveFileFromFileSrt($strFileContent, $fileName) {
		Service_Debug::setDebug();
		$arrLines = explode("\n", $strFileContent);
		echo 2;
		$i = 0;
		$textStr = '';
		foreach ($arrLines as $line) {
			$line = trim($line);
			if (is_numeric($line)) {
				if($line == 9999) break;
				continue;
			}
			if (empty($line)) continue;
			if (strpos($line, ' --> ')) continue;
			//if ($line == '<font color="#ffff00" size=14>www.tvsubtitles.net</font>')  continue;
			if ($i++) $textStr .= ' ';
			$textStr .= $line;
		}
		//$textStr = strip_tags(str_replace(array('... ...', '. . . . . .', "''", '"\\', '�'), array(' ', ' ', '"', '', "'"), $textStr));
		$textStr = strip_tags(str_replace(array('... ...', '. . . . . .', "''", '"\\'), array(' ', ' ', '"', ''), $textStr));
		//$textStr = mb_str_replace('�', "'", $textStr);
		/*
		$textStr = mb_eregi_replace('/�/' , "'", $textStr);
		$textStr = mb_eregi_replace('/et�s/', "et's", $textStr);
		$textStr = mb_eregi_replace('/’/' , "'", $textStr);
		*/
		echo 123;
		
		$stmt = "INSERT INTO `eng_file` (`file_name`, `text`) VALUES ('" . Service_Escape::escapeString($fileName) . "', '" . Service_Escape::escapeString($textStr) . "')";
		$objDB = Service_Database::instance();
		$textId = $objDB->queryInsertAutoincrement($stmt);
		
		
		$arrSenses = preg_split('/[\.\"?!\-{2}]/', $textStr, -1, PREG_SPLIT_NO_EMPTY);
		
		$arrWords1 = array();
		$arrWords2 = array();
		$arrWords3 = array();
		$arrWords4 = array();
		$arrWords5 = array();
		foreach ($arrSenses as &$sense) {
			$sense = trim($sense, ' "-.\'\\');
			if (empty($sense) || $sense == '\\') continue;
			$stmt = "UPDATE `eng_sense` SET `eng_sense_amount` = (`eng_sense_amount` + 1) WHERE `eng_sense_text` = '" . Service_Escape::escapeString($sense) . "'";
			echo $sense;
			echo '<br/>';
			if (!$objDB->query($stmt)) { // Если ничего не обновилось, значит ничего не было
				$stmt = "	INSERT INTO `eng_sense` (`eng_sense_text`) VALUES ('" . Service_Escape::escapeString($sense) . "')";
				$senseId = $objDB->queryInsertAutoincrement($stmt);
			}
			else {
				$stmt = "SELECT `ID_eng_sense` FROM `eng_sense` WHERE `eng_sense_text` = '" . Service_Escape::escapeString($sense) . "'";
				$senseId = $objDB->selectSimpleField($stmt);
			}
				
			$stmt = "
				INSERT INTO `eng_text_sense` (`ID_eng_text`, `ID_eng_sense`) VALUES ('" . $textId . "', '" . $senseId . "')
				ON DUPLICATE KEY UPDATE `amount` = (`amount` + 1);
			";
			$objDB->query($stmt);
			
			$arrWordsOfSense = array();
			$arrWordsOfSenseTemp = preg_split('/[ ,:;*=\>\<\$\&@%\[\]\(\)\/\+]/', $sense, -1, PREG_SPLIT_NO_EMPTY);
			foreach ($arrWordsOfSenseTemp as $word) {
				$word = trim($word, ' "-.\'?!\\');
				if (is_numeric($word)) continue;
				if (empty($word)) continue;
				if ($word == '\\') continue;
				//if(strlen($string))
				$arrWordsOfSense[] = $word;
			}
			$wordsInSense = sizeof($arrWordsOfSense);
			for ($k = 0; $k < $wordsInSense; $k ++) {
				$word1 = $arrWordsOfSense[$k];
				
				$stmt = "UPDATE `eng_word_1` SET `eng_word_1_amount` = (`eng_word_1_amount` + 1) WHERE `word` = '" . Service_Escape::escapeString($word1) . "'";
				if (!$objDB->query($stmt)) { // Если ничего не обновилось, значит ничего не было
					$stmt = "	INSERT INTO `eng_word_1` (`word`) VALUES ('" . Service_Escape::escapeString($word1) . "')";
					if(strpos($word1, 'view') !== false) {
						echo $word1;
						echo '<br />';
						echo $stmt;
						echo '<br />';
						echo '<br />';
					}
					$word1Id = $objDB->queryInsertAutoincrement($stmt);
				}
				else {
					$stmt = "SELECT `ID_eng_word_1` FROM `eng_word_1` WHERE `word` = '" . Service_Escape::escapeString($word1) . "'";
					$word1Id = $objDB->selectSimpleField($stmt);
				}
				
				$stmt = "
					INSERT INTO `eng_file_word_1` (`ID_eng_file`, `ID_eng_word_1`) VALUES ('" . $textId . "', '" . $word1Id . "')
					ON DUPLICATE KEY UPDATE `amount` = (`amount` + 1);
				";
				$objDB->query($stmt);
				
				$stmt = "
					INSERT INTO `eng_sense_word_1` (`ID_eng_sense`, `ID_eng_word_1`) VALUES ('" . $senseId . "', '" . $word1Id . "')
					ON DUPLICATE KEY UPDATE `amount` = (`amount` + 1);
				";
				$objDB->query($stmt);
				
				if ($k > 0) {
					$word2 = $arrWordsOfSense[$k - 1] . ' ' . $arrWordsOfSense[$k];
					
					$stmt = "UPDATE `eng_word_2` SET `eng_word_2_amount` = (`eng_word_2_amount` + 1) WHERE `word` = '" . Service_Escape::escapeString($word2) . "'";
					if (!$objDB->query($stmt)) { // Если ничего не обновилось, значит ничего не было
						$stmt = "	INSERT INTO `eng_word_2` (`word`) VALUES ('" . Service_Escape::escapeString($word2) . "')";
						$word2Id = $objDB->queryInsertAutoincrement($stmt);
					}
					else {
						$stmt = "SELECT `ID_eng_word_2` FROM `eng_word_2` WHERE `word` = '" . Service_Escape::escapeString($word2) . "'";
						$word2Id = $objDB->selectSimpleField($stmt);
					}
					
					$stmt = "
						INSERT INTO `eng_file_word_2` (`ID_eng_file`, `ID_eng_word_2`) VALUES ('" . $textId . "', '" . $word2Id . "')
						ON DUPLICATE KEY UPDATE `amount` = (`amount` + 1);
					";
					$objDB->query($stmt);
					
					$stmt = "
						INSERT INTO `eng_sense_word_2` (`ID_eng_sense`, `ID_eng_word_2`) VALUES ('" . $senseId . "', '" . $word2Id . "')
						ON DUPLICATE KEY UPDATE `amount` = (`amount` + 1);
					";
					$objDB->query($stmt);
				}
				
			
				if ($k > 1) {
					$word3 = $arrWordsOfSense[$k - 2] . ' ' . $arrWordsOfSense[$k - 1] . ' ' . $arrWordsOfSense[$k];
					
					$stmt = "UPDATE `eng_word_3` SET `eng_word_3_amount` = (`eng_word_3_amount` + 1) WHERE `word` = '" . Service_Escape::escapeString($word3) . "'";
					if (!$objDB->query($stmt)) { // Если ничего не обновилось, значит ничего не было
						$stmt = "	INSERT INTO `eng_word_3` (`word`) VALUES ('" . Service_Escape::escapeString($word3) . "')";
						$word3Id = $objDB->queryInsertAutoincrement($stmt);
					}
					else {
						$stmt = "SELECT `ID_eng_word_3` FROM `eng_word_3` WHERE `word` = '" . Service_Escape::escapeString($word3) . "'";
						$word3Id = $objDB->selectSimpleField($stmt);
					}
					
					$stmt = "
						INSERT INTO `eng_file_word_3` (`ID_eng_file`, `ID_eng_word_3`) VALUES ('" . $textId . "', '" . $word3Id . "')
						ON DUPLICATE KEY UPDATE `amount` = (`amount` + 1);
					";
					$objDB->query($stmt);
					
					$stmt = "
						INSERT INTO `eng_sense_word_3` (`ID_eng_sense`, `ID_eng_word_3`) VALUES ('" . $senseId . "', '" . $word3Id . "')
						ON DUPLICATE KEY UPDATE `amount` = (`amount` + 1);
					";
					$objDB->query($stmt);
				}
				
				if ($k > 2) {
					$word4 = $arrWordsOfSense[$k - 3] . ' ' . $arrWordsOfSense[$k - 2] . ' ' . $arrWordsOfSense[$k - 1] . ' ' . $arrWordsOfSense[$k];
						
					$stmt = "UPDATE `eng_word_4` SET `eng_word_4_amount` = (`eng_word_4_amount` + 1) WHERE `word` = '" . Service_Escape::escapeString($word4) . "'";
					if (!$objDB->query($stmt)) { // Если ничего не обновилось, значит ничего не было
						$stmt = "	INSERT INTO `eng_word_4` (`word`) VALUES ('" . Service_Escape::escapeString($word4) . "')";
						$word4Id = $objDB->queryInsertAutoincrement($stmt);
					}
					else {
						$stmt = "SELECT `ID_eng_word_4` FROM `eng_word_4` WHERE `word` = '" . Service_Escape::escapeString($word4) . "'";
						$word4Id = $objDB->selectSimpleField($stmt);
					}
						
					$stmt = "
						INSERT INTO `eng_file_word_4` (`ID_eng_file`, `ID_eng_word_4`) VALUES ('" . $textId . "', '" . $word4Id . "')
						ON DUPLICATE KEY UPDATE `amount` = (`amount` + 1);
					";
					$objDB->query($stmt);
						
					$stmt = "
						INSERT INTO `eng_sense_word_4` (`ID_eng_sense`, `ID_eng_word_4`) VALUES ('" . $senseId . "', '" . $word4Id . "')
						ON DUPLICATE KEY UPDATE `amount` = (`amount` + 1);
					";
					$objDB->query($stmt);
				}
				
				/*
				if (!isset($arrWords1[$word1])) $arrWords1[$word1] = 1;
				else $arrWords1[$word1]++;
				if ($k > 0) {
					$word2 = $arrWordsOfSense[$k - 1] . ' ' . $arrWordsOfSense[$k];
					if (!isset($arrWords2[$word2])) $arrWords1[$word2] = 1;
					else $arrWords2[$word2]++;
				} 
				*/
			}
		}
		
		
		return $textStr;
	}
	
	/**
	 * Считывает папку с файлами
	 * 
	 * @param string $strFolderPath - путь до папки с файлами
	 */
	public function readDirWithFiles($strFolderPath) {
		Service_Debug::setDebug();
		echo 3;
		$handle = opendir($strFolderPath);
		$objDB = Service_Database::instance();
		if ($handle) {
			$wholeText = '';
			$i = 0;
			while (false !== ($entry = readdir($handle))) {
				$filePath = $strFolderPath . $entry;
				if (!is_file($filePath)) continue;
				if (++$i < 10) continue;
				//if ($i >= 10) continue;
				$fileHandle = fopen($filePath, "rb");
				$contents = fread($fileHandle, filesize($filePath));
				fclose($fileHandle);
				$currentText .= $this->saveFileFromFileSrt($contents, $entry);
				$wholeText .= $currentText;
				// Разбиваем текст на предложения
				
				echo $entry . '<br />';;
				//echo $currentText;
				//if ($i++ == 4) break;
			}
			closedir($handle);
			/*
			echo $wholeText;
			//$wholeText = iconv('UTF-8', 'cp1251', $wholeText);
			$simbolsAmount = strlen($wholeText);
			$arrSimbols = array();
			for ($i = 0; $i < $simbolsAmount; $i++) {
				$simbol = strtolower($wholeText{$i});
				echo $simbol;
				if (!isset($arrSimbols[$simbol])) $arrSimbols[$simbol] = 1;
				else $arrSimbols[$simbol]++;
			}
			asort($arrSimbols);
			echo '<pre>';
				print_r($arrSimbols);
			echo '</pre>';
			foreach ($arrSimbols as $key => $val) {
				//if (ord($key) <= 128) CONTINUE;
					echo $key . ' = ' . ord($key). ' = ' . $val . '<br />';
			}
			*/
		}
	}
	

	/**
	 * Метод задает стандартные типы для предварительно заданных слов
	 */
	public function setStandardTypes() {
		Service_Debug::setDebug();
		$arrAllTypes = array();
		$arrAllTypes[1] = $this->arrPronouns;
		$arrAllTypes[2] = $this->arrArticles;
		$arrAllTypes[3] = $this->arrNumbers;
		$objDB = Service_Database::instance();
		foreach ($arrAllTypes as $type => $arrWords) {
			$stmt = "UPDATE `eng_word_1` SET `word_type` = '" . $type . "' WHERE `word` IN ('" . implode("', '", $arrWords) . "')";
			echo $type;
			echo ' - ';
			echo $objDB->query($stmt);
			echo '<br />';
		}
	}
	
	/**
	 * Удаляет слова, содержащиеся в файле
	 * 
	 * @param integer $idFile
	 */
	public function deleteFileWords($idFile) {
		if (!$idFile) return false;
		$objDB = Service_Database::instance();
		$stmt = "SELECT FROM `eng_sense_word_4` WHERE ";
	}
	
}
?>